// pages/zhanghuInfo/zhanghuInfo.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    textLength1: 0,
    val1: '',
    textLength2: 0,
    val2: '',
    tupianList: [],
    imgURL2: '', //头像
    imgURL: '',
    shopBean: '', //厂家信息
    xiangmlist: '', //项目列表
    zhuyingxm: '',
    carId: ''
  },
  // 获取项目列表
  getfenllist() {
    console.log('列表')
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "shopTypeList"
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        that.setData({
          xiangmlist: res.data.shopTypeList
        })
        that.getshangjiainfo()
      }
    });
  },
  //厂家信息
  getshangjiainfo() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getShopInfo",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          let zhuyingxm
          for (var index1 in that.data.xiangmlist) {
            if (that.data.xiangmlist[index1].shopTypeId == res.data.shopBean.shopTypeId) {
              zhuyingxm = that.data.xiangmlist[index1].shopTypeName
            }
          }
          that.setData({
            imgURL2: res.data.shopBean.smallLogoUrl,
            shopBean: res.data.shopBean,
            zhuyingxm: zhuyingxm,
            carId: res.data.shopBean.cardNum.substring(0, 6) + '********' + res.data.shopBean.cardNum.substring(14)
          })
          setTimeout(function() {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
        }
      }
    });
  },
  changlogo() {
    let that = this
    wx.navigateTo({
      url: '/pages/setting/page/cropper/cropper',
    })
  },
  addImg() {
    let that = this
    let oldtupianList = that.data.tupianList
    let num = 3 - oldtupianList.length
    let newtupianList
    wx.chooseImage({
      count: num,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        newtupianList = oldtupianList.concat(tempFilePaths)
        console.log(newtupianList)
        that.setData({
          tupianList: newtupianList
        })
      }
    })

  },
  shanchu(e) {
    let indexs = e.currentTarget.dataset.index
    let tupianList = this.data.tupianList
    tupianList.splice(indexs, 1)
    this.setData({
      tupianList: tupianList
    })
  },
  setval1(e) {
    console.log(e)
    this.setData({
      textLength1: e.detail.cursor,
      val1: e.detail.value
    })
  },
  setval2(e) {
    console.log(e)
    this.setData({
      textLength2: e.detail.cursor,
      val2: e.detail.value
    })
  },
  // 图片预览
  previewImage: function(e) {
    let that = this
    var current = e.target.dataset.src;
    console.log(current)
    wx.previewImage({
      current: current, // 当前显示图片的http链接  
      urls: [that.data.imgURL2] // 需要预览的图片http链接列表  
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  // 更换头像
  changeChatup() {
    console.log(55)
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "updShopLogo",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
        shopLogo: that.data.imgURL
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        setTimeout(function() {
          wx.hideLoading()
        }, 0)
        setTimeout(function() {
          wx.showToast({
            title: res.data.resultNote,
            icon: 'none',
            duration: 1500
          })
        }, 0)
        that.getfenllist()
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    if (that.data.imgURL) {
      // 更换头像
      that.changeChatup()
    } else {
      that.getfenllist()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    let that = this
    that.setData({
      imgURL: ''
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    let that = this
    that.setData({
      imgURL: ''
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})